package com.example.apple.hxbtc.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.apple.hxbtc.R;
import com.example.apple.hxbtc.bean.HongKong;
import com.example.apple.hxbtc.bean.Korbit;
import com.example.apple.hxbtc.bean.Ouyuan;
import com.example.apple.hxbtc.bean.Riyuan;
import com.example.apple.hxbtc.bean.USA;
import com.example.apple.hxbtc.bean.Yingbang;
import com.example.apple.hxbtc.utils.MathUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static android.R.id.button1;

/**
 * Created by hpz on 17/2/22.
 */
public class MessageFragment extends Fragment implements View.OnClickListener{

    private TextView rate_one;
    private TextView rate_two;
    private TextView rate_three;
    private TextView rate_four;
    private TextView rate_five;
    private TextView rate_six;

    private Timer timer1;
    private Button btn1;
    static FragmentActivity mActivity;

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 0:
                    /*String str0 = (String) msg.obj;
                    double ss0 = Math.change_two(str0);
                    if (!TextUtils.isEmpty(usas)) {
                        double usass = Math.change_two(usas);
                        rate_one.setText(Math.change_three(ss0 * usass +""));
                    }*/
                    String s0 = (String) msg.obj;
                    String b0 = MathUtil.change_one(s0);
                    rate_one.setText(b0 +"");
                    break;
                case 1:
                    String s1 = (String) msg.obj;
                    String b1 = MathUtil.change_one(s1);
                    rate_two.setText(b1 +"");
                    break;
                case 2:
                    String s2 = (String) msg.obj;
                    String b2 = MathUtil.change_one(s2);
                    rate_three.setText(b2 +"");
                    break;
                case 3:
                    String s3 = (String) msg.obj;
                    String b3 = MathUtil.change_one(s3);
                    rate_four.setText(b3 +"");
                    break;
                case 4:
                    String s4 = (String) msg.obj;
                    String b4 = MathUtil.change_one(s4);
                    rate_five.setText(b4 +"");
                    break;
                case 5:
                    String s5 = (String) msg.obj;
                    String b5 = MathUtil.change_one(s5);
                    rate_six.setText(b5 +"");
                    break;
            }
            super.handleMessage(msg);
        }
    };

    private String usas;
    private String ous;
    private String hks;
    private String rys;
    private String ybs;
    private String kbs;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = (FragmentActivity) activity;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        rate_one = (TextView) getActivity().findViewById(R.id.rate_one);
        rate_two = (TextView) getActivity().findViewById(R.id.rate_two);
        rate_three = (TextView) getActivity().findViewById(R.id.rate_three);
        rate_four = (TextView) getActivity().findViewById(R.id.rate_four);
        rate_five = (TextView) getActivity().findViewById(R.id.rate_five);
        rate_six = (TextView) getActivity().findViewById(R.id.rate_six);
        btn1 = (Button) getActivity().findViewById(button1);
        timer1 = new Timer();
        timer1.schedule(new TimerTask() {
            @Override
            public void run() {
                getRateOne();
            }
        }, 1000, 10 * 60 * 1000);//1000表示开始时1秒后发送一次，10*10000表示每10秒后发送一次
    }
    private void getRateOne() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url("http://web.juhe.cn:8080/finance/exchange/rmbquot?key=f01cc1baf6e09576cb0f10b44d6f9b75")
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    parseJSONWithGSONRateOne(responseData);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }
    private void parseJSONWithGSONRateOne(String jsonData) {

        try {
            JSONObject object = new JSONObject(jsonData);
            JSONArray result = object.getJSONArray("result");
            for (int i=0;i<result.length();i++) {
                JSONObject obj = (JSONObject) result.get(i);
                JSONObject obj1 = obj.getJSONObject("data1");
                USA usa = new USA();
                usa.setfSellPri(obj1.getString("fSellPri"));
                Message message1 = new Message();
                message1.what = 0;
                usas = usa.getfSellPri();
                message1.obj = usa.getfSellPri();
                mHandler.sendMessage(message1);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            JSONObject object = new JSONObject(jsonData);
            JSONArray result = object.getJSONArray("result");
            for (int i=0;i<result.length();i++) {
                JSONObject obj = (JSONObject) result.get(i);
                JSONObject obj1 = obj.getJSONObject("data2");
                Ouyuan ou = new Ouyuan();
                ou.setfSellPri(obj1.getString("fSellPri"));
                Message message1 = new Message();
                message1.what = 1;
                ous = ou.getfSellPri();
                message1.obj = ou.getfSellPri();
                mHandler.sendMessage(message1);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            JSONObject object = new JSONObject(jsonData);
            JSONArray result = object.getJSONArray("result");
            for (int i=0;i<result.length();i++) {
                JSONObject obj = (JSONObject) result.get(i);
                JSONObject obj1 = obj.getJSONObject("data3");
                HongKong hk = new HongKong();
                hk.setfSellPri(obj1.getString("fSellPri"));
                Message message1 = new Message();
                message1.what = 2;
                hks = hk.getfSellPri();
                message1.obj = hk.getfSellPri();
                mHandler.sendMessage(message1);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            JSONObject object = new JSONObject(jsonData);
            JSONArray result = object.getJSONArray("result");
            for (int i=0;i<result.length();i++) {
                JSONObject obj = (JSONObject) result.get(i);
                JSONObject obj1 = obj.getJSONObject("data4");
                Riyuan ry = new Riyuan();
                ry.setfSellPri(obj1.getString("fSellPri"));
                Message message1 = new Message();
                message1.what = 3;
                rys = ry.getfSellPri();
                message1.obj = ry.getfSellPri();
                mHandler.sendMessage(message1);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            JSONObject object = new JSONObject(jsonData);
            JSONArray result = object.getJSONArray("result");
            for (int i=0;i<result.length();i++) {
                JSONObject obj = (JSONObject) result.get(i);
                JSONObject obj1 = obj.getJSONObject("data5");
                Yingbang yb = new Yingbang();
                yb.setfSellPri(obj1.getString("fSellPri"));
                Message message1 = new Message();
                message1.what = 4;
                ybs = yb.getfSellPri();
                message1.obj = yb.getfSellPri();
                mHandler.sendMessage(message1);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            JSONObject object = new JSONObject(jsonData);
            JSONArray result = object.getJSONArray("result");
            for (int i=0;i<result.length();i++) {
                JSONObject obj = (JSONObject) result.get(i);
                JSONObject obj1 = obj.getJSONObject("data20");
                Korbit kb = new Korbit();
                kb.setfSellPri(obj1.getString("fSellPri"));
                Message message1 = new Message();
                message1.what = 5;
                kbs = kb.getfSellPri();
                message1.obj = kb.getfSellPri();
                mHandler.sendMessage(message1);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_message,container,false);
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (timer1 != null) {
            timer1.cancel();
        }
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button1:
                rate_one.setText("");
        }
    }
}
